/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package workstationbookerweb.fragmentos;

import Fachada.Fachada;
import Servicios.ValidacionDatosEntrada;
import Singletons.Mensajes;
import VO.AdministradorVO;
import com.sun.rave.web.ui.appbase.AbstractFragmentBean;
import com.sun.webui.jsf.component.Button;
import com.sun.webui.jsf.component.Label;
import com.sun.webui.jsf.component.RadioButtonGroup;
import com.sun.webui.jsf.component.TextField;
import com.sun.webui.jsf.model.SingleSelectOptionsList;
import javax.faces.FacesException;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceException;
import workstationbookerweb.ApplicationBean1;
import workstationbookerweb.SessionBean1;
import workstationbookerweb.RequestBean1;

/**
 * <p>Fragment bean that corresponds to a similarly named JSP page
 * fragment.  This class contains component definitions (and initialization
 * code) for all components that you have defined on this fragment, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version fragmentoBuscarAdministrador.java
 * @version Created on May 31, 2011, 10:21:36 PM
 * @author Jhovany Amaya
 */

public class fragmentoBuscarAdministrador extends AbstractFragmentBean {
    Fachada fachada;

    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

     /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
        opcionesRBGDefaultOptions.setOptions(new com.sun.webui.jsf.model.Option[]{new com.sun.webui.jsf.model.Option("1", "Nombre"), new com.sun.webui.jsf.model.Option("2", "C\u00e9dula")});
        fachada= Fachada.getInstance();
        opcionesRBGDefaultOptions.setSelectedValue("1");
    }
    private AdministradorVO [] avoTabla;

    public AdministradorVO[] getAvoTabla() {
        return avoTabla;
    }

    public void setAvoTabla(AdministradorVO[] avoTabla) {
        this.avoTabla = avoTabla;
    }

    private SingleSelectOptionsList opcionesRBGDefaultOptions = new SingleSelectOptionsList();

    public SingleSelectOptionsList getOpcionesRBGDefaultOptions() {
        return opcionesRBGDefaultOptions;
    }

    public void setOpcionesRBGDefaultOptions(SingleSelectOptionsList ssol) {
        this.opcionesRBGDefaultOptions = ssol;
    }
    private RadioButtonGroup opcionesRBG = new RadioButtonGroup();

    public RadioButtonGroup getOpcionesRBG() {
        return opcionesRBG;
    }

    public void setOpcionesRBG(RadioButtonGroup rbg) {
        this.opcionesRBG = rbg;
    }
    private TextField parametroTF = new TextField();

    public TextField getParametroTF() {
        return parametroTF;
    }

    public void setParametroTF(TextField tf) {
        this.parametroTF = tf;
    }
    private Button buscarB = new Button();

    public Button getBuscarB() {
        return buscarB;
    }

    public void setBuscarB(Button b) {
        this.buscarB = b;
    }
    private Button cancelarB = new Button();

    public Button getCancelarB() {
        return cancelarB;
    }

    public void setCancelarB(Button b) {
        this.cancelarB = b;
    }
    private Label mensajeL = new Label();

    public Label getMensajeL() {
        return mensajeL;
    }

    public void setMensajeL(Label l) {
        this.mensajeL = l;
    }
    
    // </editor-fold>

    public fragmentoBuscarAdministrador() {
    }

    /**
     * <p>Callback method that is called whenever a page containing
     * this page fragment is navigated to, either directly via a URL,
     * or indirectly via page navigation.  Override this method to acquire
     * resources that will be needed for event handlers and lifecycle methods.</p>
     * 
     * <p>The default implementation does nothing.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
        
        
        // <editor-fold defaultstate="collapsed" desc="Visual-Web-managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called.  Override this
     * method to release resources acquired in the <code>init()</code>
     * resources that will be needed for event handlers and lifecycle methods.</p>
     * 
     * <p>The default implementation does nothing.</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    public String buscarB_action() {
        try{
            AdministradorVO avo;
            ValidacionDatosEntrada vde= new ValidacionDatosEntrada();
            String mensaje;
            if (!camposvacios()){
                if (opcionesRBG.getSelected().toString().equals("1")){
                    mensaje = vde.nombreCorrecto(parametroTF.getText().toString());
                    if (mensaje.equals(Mensajes.ENTRADA_VALIDA)){
                        avo=fachada.leerAdministrador(parametroTF.getText().toString().trim(),0L);
                        if (avo!=null){
                            avoTabla = new AdministradorVO[1];
                            avoTabla[0]=avo;
                            mensajeL.setText(Mensajes.ADMINISTRADOR_ENCONTRADO);
                        }else{
                            mensajeL.setText(Mensajes.ERROR_ADMINISTRADOR_INEXISTENTE);

                        }
                    }else{
                        mensajeL.setText(mensaje);
                    }
                }else{
                    mensaje= vde.cedulaCorrecta(parametroTF.getText().toString());
                    if (mensaje.equals(Mensajes.ENTRADA_VALIDA)){
                        avo=fachada.leerAdministrador(null,Long.parseLong(parametroTF.getText().toString().trim()));
                        if (avo!=null){
                            avoTabla = new AdministradorVO[1];
                            avoTabla[0]=avo;
                            mensajeL.setText(Mensajes.ADMINISTRADOR_ENCONTRADO);
                        }else{
                            mensajeL.setText(Mensajes.ERROR_ADMINISTRADOR_INEXISTENTE);
                        }
                    }else{
                        mensajeL.setText(mensaje);
                    }
                }
            }else{
                mensajeL.setText(Mensajes.ERROR_CAMPOS_VACIOS);

            }
        }catch (NonUniqueResultException nure){
            mensajeL.setText(Mensajes.ERROR_ADMINISTRADORES_HOMONIMOS);
        }catch (NoResultException nre){
            mensajeL.setText(Mensajes.ERROR_ADMINISTRADOR_INEXISTENTE);
        }catch (PersistenceException pe){
           mensajeL.setText(Mensajes.ERROR_CONEXION_BD);
        }
        return null;
    }

    /**
     * <p>Verifica que los campos necesarios no se encuentren vacios</p>
     *
     * @return Verdadero: Si algun(nos) campo(s) esta(n) vacio(s). -
     * Falso: Si ningun campo esta vacio.
     */
    private boolean camposvacios() {
        boolean a=false;
        try {
            if(parametroTF.getText().toString().equals("")){
            a=true;
            }else{
                a=false;
            }
        }catch (Exception e){
            a=true;
        }
        return a;
    }

    public String cancelarB_action() {
        return "/faces/paginas/OpcionesSuperUsuario.jsp";
    }

}
